<?php
class Modelo_ec extends Model {
    public $product_id;
    public $product_name;
    public $product_seo;
    public $product_desc;
    public $product_price;
    public $product_sku;
    public $vendor_id;
    public $product_in_stock;
    public $product_full_image;
    public $product_discount_id;
    public $product_publish;
    public $custom_attribute;
    public $category_name;
    public $category_parent_id;

    public $desc_s;
    public $desc;
    public $desc_s_english;
    public $desc_english;
    public $desc_s_italy;
    public $desc_italy;
    public $desc_s_portugues;
    public $desc_portugues;

    public $banners;
    public $lista_categorias = array();
    public $lista_sub_categorias = array();

    function Modelo_ec() {
        parent::Model();
    }

    /**
     * Llena variables con datos del producto
     */
    function get_articulo($id) { //pasar a moldel de ecommerce
        $this->load->library('session');
        $query = $this->db->query("SELECT * FROM product WHERE product_id='".$id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $this->product_sku          = $row->product_sku;
            $this->product_seo          = $row->product_desc;
            $this->product_name         = $row->product_name;
            $this->vendor_id            = $row->vendor_id;
            $this->product_in_stock     = $row->product_in_stock;
            $this->product_full_image   = $row->product_full_image;
            $this->product_discount_id  = $row->product_discount_id;
            $this->product_publish      = $row->product_publish;
            $this->custom_attribute     = $row->custom_attribute;
        }
        $this->product_id     = $id;

        //calcullo de precio
        $query = $this->db->query("SELECT product_price FROM product_price WHERE product_id='".$id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $precio = $row->product_price;
        }

        $query = $this->db->query("SELECT category_id FROM product_category_xref WHERE product_id='".$id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $category_id = $row->category_id;
        }

        $query = $this->db->query("SELECT category_parent_id FROM category_xref WHERE category_child_id='".$category_id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $category_parent_id = $row->category_parent_id;
        }

        //echo "SELECT products_per_row FROM category WHERE category_id='".$category_id."'";
        $query = $this->db->query("SELECT products_per_row FROM category WHERE category_id='".$category_id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $ganancia_categoria = $row->products_per_row;
        }

        $query = $this->db->query("SELECT products_per_row FROM category WHERE category_id='".$category_parent_id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $ganancia_sub_categoria = $row->products_per_row;
        }

        $ganancia = (($ganancia_categoria + $ganancia_sub_categoria) / 100) + 1;
        $this->product_price = $precio * $ganancia;

        

// echo $precio.'mas'.$ganancia;
        //fin de calculo de precio

        $query = $this->db->query("SELECT * FROM product_text WHERE product_id='".$id."' AND idioma='".$this->session->userdata('lenguaje')."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $this->product_s_desc = $row->product_s_desc;
            $this->product_desc   = $row->product_desc;
        }


        $query = $this->db->query("SELECT * FROM product_text WHERE product_id='".$id."'");
        foreach ($query->result() as $row) {
            if($row->idioma == 'spanish') {
                $this->desc_s = $row->product_s_desc;
                $this->desc   = $row->product_desc;
            }elseif($row->idioma == 'english') {
                $this->desc_s_english = $row->product_s_desc;
                $this->desc_english   = $row->product_desc;
            }elseif($row->idioma == 'italy') {
                $this->desc_s_italy = $row->product_s_desc;
                $this->desc_italy   = $row->product_desc;
            }elseif($row->idioma == 'portugues') {
                $this->desc_s_portugues = $row->product_s_desc;
                $this->desc_portugues   = $row->product_desc;
            }
        }

    }


    /**
     * Llena variables con datos del producto con SEO
     */
    function get_articulo_seo($id) { //pasar a moldel de ecommerce
        //Agregado SEO
        $query = $this->db->query("SELECT product_id FROM product WHERE product_desc='".$id."'");
        $row1=$query->row();
        $id=$row1->product_id;

        $this->load->library('session');
        $query = $this->db->query("SELECT * FROM product WHERE product_id='".$id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $this->product_sku          = $row->product_sku;
            $this->product_name         = $row->product_name;
            $this->vendor_id            = $row->vendor_id;
            $this->product_in_stock     = $row->product_in_stock;
            $this->product_full_image   = $row->product_full_image;
            $this->product_discount_id  = $row->product_discount_id;
            $this->product_publish      = $row->product_publish;
            $this->custom_attribute     = $row->custom_attribute;
        }
        $this->product_id     = $id;

        //calcullo de precio
        $query = $this->db->query("SELECT product_price FROM product_price WHERE product_id='".$id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $precio = $row->product_price;
        }

        $query = $this->db->query("SELECT category_id FROM product_category_xref WHERE product_id='".$id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $category_id = $row->category_id;
        }

        $query = $this->db->query("SELECT category_parent_id FROM category_xref WHERE category_child_id='".$category_id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $category_parent_id = $row->category_parent_id;
        }

        //echo "SELECT products_per_row FROM category WHERE category_id='".$category_id."'";
        $query = $this->db->query("SELECT products_per_row FROM category WHERE category_id='".$category_id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $ganancia_categoria = $row->products_per_row;
        }

        $query = $this->db->query("SELECT products_per_row FROM category WHERE category_id='".$category_parent_id."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $ganancia_sub_categoria = $row->products_per_row;
        }

        $ganancia = (($ganancia_categoria + $ganancia_sub_categoria) / 100) + 1;
        $this->product_price = $precio * $ganancia;
        //echo $precio.'mas'.$ganancia;
        //fin de calculo de precio

        $query = $this->db->query("SELECT * FROM product_text WHERE product_id='".$id."' AND idioma='".$this->session->userdata('lenguaje')."'");
        if ($query->num_rows() > 0) {
            $row=$query->row();
            $this->product_s_desc = $row->product_s_desc;
            $this->product_desc   = $row->product_desc;
        }


        $query = $this->db->query("SELECT * FROM product_text WHERE product_id='".$id."'");
        foreach ($query->result() as $row) {
            if($row->idioma == 'spanish') {
                $this->desc_s = $row->product_s_desc;
                $this->desc   = $row->product_desc;
            }elseif($row->idioma == 'english') {
                $this->desc_s_english = $row->product_s_desc;
                $this->desc_english   = $row->product_desc;
            }elseif($row->idioma == 'italy') {
                $this->desc_s_italy = $row->product_s_desc;
                $this->desc_italy   = $row->product_desc;
            }elseif($row->idioma == 'portugues') {
                $this->desc_s_portugues = $row->product_s_desc;
                $this->desc_portugues   = $row->product_desc;
            }
        }

    }



    /**
     * Retorna datos del vendedor
     */
    function get_vendedor($id) { //pasar a moldel de ecommerce
        $query = $this->db->query("SELECT * FROM vendor WHERE vendor_id='".$id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $this->product_price= $row->product_price;
    }

    /**
     * Retorna precio del articulo
     */
    function get_precio($id) { //pasar a moldel de ecommerce
        $query = $this->db->query("SELECT product_price FROM product_price WHERE product_id='".$id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $this->product_price= $row->product_price;
    }

    /**
     * Devuelve array con listado de categorias padre
     */
    function get_lista_categorias() { //pasar a modelo categorias
        $consulta2 = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id = 0");
        foreach ($consulta2->result() as $row) {
            $query = $this->db->query("SELECT category_id FROM category WHERE category_id=".$row->category_child_id);
            $row2=$query->row();
            $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id=".$row2->category_id." AND idioma='".$this->session->userdata('lenguaje')."'");
            $row3=$query->row();
            $this->lista_categorias[$row2->category_id]=$row3->category_desc;
        }
    }

    /**
     * Devuelve array con listado de sub_categorias
     */
    function get_lista_sub_categorias($id) { //pasar a modelo categorias
        $consulta2 = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id =$id");
        $row=$consulta2->row();

        foreach ($consulta2->result() as $row) {
            $query = $this->db->query("SELECT category_id FROM category WHERE category_id=".$row->category_child_id);
            $row2=$query->row();
            $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id=".$row2->category_id." AND idioma='".$this->session->userdata('lenguaje')."'");
            $row3=$query->row();
            $this->lista_sub_categorias[$row2->category_id]=$row3->category_desc;
        }
    }

    /**
     * Retorna nombre de la categoria segun id del producto se usa para el frontend
     */
    function get_nombre_categoria($id) { //pasar a modelo categorias
        //$query = $this->db->query("SELECT category_id FROM product_category_xref WHERE product_id=".$id);
        //echo "SELECT category_id FROM product_category_xref WHERE product_id=".$id;
        //$row2=$query->row();
        //$query = $this->db->query("SELECT category_id FROM category WHERE category_id=".$row2->category_id);
        $query = $this->db->query("SELECT category_id FROM category WHERE category_id=".$id);//$row2->category_id);
        $row2=$query->row();
        $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id=".$row2->category_id." AND idioma='".$this->session->userdata('lenguaje')."'");
        $row3=$query->row();
        $this->category_name = $row3->category_desc;
    }

    /**
     * Retorna nombre de la categoria segun id de la categoria se usa para backend
     */
    // esta llegando el id del producto tendria que estar llegando el id de la categoria ver :-)
    function get_nombre_categoria_bn($id) { //pasar a modelo categorias
        //$query = $this->db->query("SELECT category_id FROM product_category_xref WHERE product_id=".$id);
        //$row2=$query->row();
        $query = $this->db->query("SELECT category_id FROM category WHERE category_id=".$id);//$row2->category_id);
        $row2=$query->row();
        $query = $this->db->query("SELECT category_desc FROM category_text WHERE category_id=".$row2->category_id." AND idioma='".$this->session->userdata('lenguaje')."'");
        $row3=$query->row();
        $this->category_name = $row3->category_desc;
    }


    /**
     * Retorna nombre de la categoria
     */
    function get_id_categoria_padre($id) { //pasar a modelo categorias
        $query = $this->db->query("SELECT category_parent_id FROM category_xref WHERE category_child_id='".$id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $this->category_parent_id = $row->category_parent_id;
        return $this->category_parent_id;
    }


    /**
     * saveCar() - Graba el contenido del carrito en la base de datos.
     */
    function saveCar($data) { //pasar a modelo ecommerce
        $this->get_articulo($data['product_id']);
        $order_subtotal=0;
        $precio = $this->product_price;
        $order_subtotal = $precio*$data['quantity'];

        $order_total = $order_subtotal;
        $bonificacion = 0;
        $bonificacion2 = 0;
        $iva = 0;
        $percepcion = 0;
        $total = $order_total;
        $query = $this->db->query("INSERT INTO orders set user_id='".$this->session->userdata('id')."', vendor_id='1', order_number='', order_total='".$total."', order_subtotal='".$order_total."', order_tax='".$iva."', order_discount='".$bonificacion."', order_status='P', cdate=".time().", mdate=".time().", customer_note='".$data['customer_note']."', orden_compra='".$data['orden_compra']."', ip_address='".$_SERVER['REMOTE_ADDR']."'");

        //ultimo registro ingresado
        $nuerodeorden = $this->db->insert_id();
        $query = $this->db->query("INSERT INTO order_item set order_id='".$nuerodeorden."', vendor_id='1', product_id='".$data['product_id']."', order_item_sku='".$this->product_sku."', order_item_name='".$this->product_name."', product_quantity='".$data['quantity']."', product_item_price='".$this->product_price."', order_status='P', cdate=".time().", mdate=".time()."");
        return $nuerodeorden;
    }

    /**
     * Retorna datos listado de categorias
     */
    function get_categorias() { //pasar a modelo categorias
        $query['consulta2'] = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id = 0 ORDER BY category_child_id");
        return $query;
    }

    /**
     * Retorna datos para el banner id es el numero de banner a mostrar
     */
    function get_banners($id) { //pasar a modelo banners
        $query = $this->db->query("SELECT texto FROM banners WHERE peso ='".$id."'");
        if ($query->num_rows() > 0)
            $row=$query->row();
        $content = $row->texto;
        $sustituye = array("(\r\n)", "(\n\r)", "(\n)", "(\r)");
        $content = preg_replace($sustituye, "", $content);
        $this->banners = $content;
    }


    /**
     * Retorna datos para el menu, listao de noticias y listado de categorias
     */
    function get_menu() { //pasar a modelo menu
        $query['consulta'] = $this->db->query("SELECT * FROM noticias ORDER BY peso ASC");
        //        $query['consulta2'] = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id = 0");
        return $query;
    }

    /**
     * Retorna datos para el contenido menu, listao de curoso
     */
    function get_cursos() { //pasar a modelo cursos
        $query = $this->db->query("SELECT * FROM cursos ORDER BY peso ASC");
        //echo "SELECT * FROM cursos ORDER BY peso ASC";
        //        $query['consulta2'] = $this->db->query("SELECT category_child_id FROM category_xref WHERE category_parent_id = 0");
        return $query;
    }


    /**
     * Retorna datos para el contenido del home
     * lista productos en ofertas por ahora ...
     */
    function get_contenido_home() { //ver
        $query['consulta']  = $this->db->query("SELECT product_id FROM product WHERE product_discount_id = 1");
        $query['consulta1'] = $this->db->query("SELECT product_id FROM product WHERE product_discount_id = 2");
        $query['consulta2'] = $this->get_cursos();
        // poner otra cosulta para los mas vendidos por ahora lo sacamos con product_discont igual a 2
        return $query;
    }


}
?>
